{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# netcdf - [1] netcdf.Dataset\n",
    "#\n",
    "import netCDF4 as nc\n",
    "\n",
    "fname = \"E:\\\\sfxData\\\\NetCDF\\\\div1980.nc\"\n",
    "ds = nc.Dataset(fname)\n",
    "\n",
    "print('===数据集')\n",
    "print(ds)\n",
    "print()\n",
    "\n",
    "#  查看nc数据各个变量的信息\n",
    "print('===查看nc数据各个变量的信息')\n",
    "for key in ds.variables.keys():\n",
    "    print('___________________________________________')\n",
    "    print(key)\n",
    "    print(ds.variables[key])\n",
    "\n",
    "# 数据\n",
    "# current shape = (1464, 10, 51, 71)\n",
    "print()\n",
    "print('===查看nc数据')\n",
    "data = ds.variables['d']\n",
    "print(data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# netcdf\n",
    "#\n",
    "import os\n",
    "import netCDF4 as nc\n",
    "\n",
    "# print(nc.__version__)\n",
    "\n",
    "fname = \"../../data/FY4A_L2_DLR_20190306_4000M.NC\"\n",
    "os.path.exists(fname)\n",
    "\n",
    "ds = nc.Dataset(fname, 'r', format='netCDF4')\n",
    "# print(ds)\n",
    "# print(ds.dimensions.keys())\n",
    "# print(ds.variables.keys())\n",
    "\n",
    "# print(ds.variables['x'])\n",
    "# print(ds.variables['y'])\n",
    "# print(ds.variables['DLR'])\n",
    "\n",
    "dlr = ds.variables['DLR']\n",
    "print(dlr.shape)\n",
    "print(dlr[0, 0])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# netcdf - [2] xarray.Dataset\n",
    "#\n",
    "import xarray as xr\n",
    "import numpy as np\n",
    "\n",
    "fname: str = \"E:\\\\sfxData\\\\NetCDF\\\\div1980.nc\"\n",
    "ds: xr.Dataset = xr.open_dataset(fname, engine='netcdf4')\n",
    "\n",
    "print(ds)\n",
    "print()\n",
    "\n",
    "da: xr.DataArray = ds['longitude']\n",
    "print(da)\n",
    "print()\n",
    "\n",
    "v: np.ndarray = da.values\n",
    "print(v)\n",
    "print()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ecmwf - xarry.engine = netcdf4\n",
    "#\n",
    "import os\n",
    "import xarray as xr\n",
    "\n",
    "\n",
    "def nc_file(fname: str):\n",
    "    \"\"\" 读取 netCDF4 文件 \"\"\"\n",
    "    if not os.path.exists(fname):\n",
    "        print(\"文件不存在 - \", fname)\n",
    "    else:\n",
    "        ds = xr.open_dataset(fname, engine='netcdf4')\n",
    "        print(ds)\n",
    "        pass\n",
    "\n",
    "    return\n",
    "\n",
    "\n",
    "#\n",
    "fname = r\"E:\\sfxData\\ECMWF\\div1980.nc\"\n",
    "nc_file(fname)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "cf18841ace8313d0bc088ca146c17a6c0040e82121d5cb75c0ea07172309253d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
